package com.xuerhua.yxt.controller.teacher;

import java.util.ArrayList;
import java.util.List;

import com.xuerhua.yxt.model.TeacherInfo;
import com.xuerhua.yxt.reqVo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xuerhua.yxt.common.AppUser;
import com.xuerhua.yxt.common.Const;
import com.xuerhua.yxt.common.MenuConst;
import com.xuerhua.yxt.common.RspResult;
import com.xuerhua.yxt.common.enums.GroupType;
import com.xuerhua.yxt.common.enums.Status;
import com.xuerhua.yxt.controller.BaseController;
import com.xuerhua.yxt.model.Grid;
import com.xuerhua.yxt.model.GroupInfo;
import com.xuerhua.yxt.model.StudentRoutineParam;
import com.xuerhua.yxt.page.OrderBy;
import com.xuerhua.yxt.page.Page;
import com.xuerhua.yxt.service.IClassPointService;
import com.xuerhua.yxt.service.IGroupInfoService;
import com.xuerhua.yxt.service.IStudentPointService;
import com.xuerhua.yxt.service.IStudentRoutineParamService;
import com.xuerhua.yxt.service.IStudentRoutineService;
import com.xuerhua.yxt.view.StudentRoutineView;
import com.xuerhua.yxt.vo.PointsQueryVO;

@RequestMapping(Const.TEACHER_ROLE + MenuConst.STUDENT_POINTS)
@Controller
public class StudentPointsController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(StudentPointsController.class);

    @Autowired
    private IStudentRoutineParamService studentRoutineParamService;
    @Autowired
    private IStudentRoutineService studentRoutineService;
    @Autowired
    private IGroupInfoService groupInfoService;
    @Autowired
    private IStudentPointService studentPointService;
    @Autowired
    private IClassPointService classPointService;

    @RequestMapping(value = {"", "/"})
    public String index(Model model) {
        AppUser appUser = getAppUser();
        if (("," + appUser.getTeacher().getPosition() + ",").indexOf(",5,") != -1) {
            model.addAttribute("isAdmin", true);
        }
        model.addAttribute("isJudgeTeacher", studentRoutineParamService.
                isJudgeTeacher(appUser.getTeacher().getId(), appUser.getTeacher().getSchoolId()));
        return "studentPoints/index";
    }

    /**
     * 学生常规登记
     */
    @RequestMapping("register")
    public String register() {
        return "studentPoints/register";
    }

    /**
     * 类别列表
     *
     * @return
     */
    @RequestMapping("typeList")
    public String typeList(Model model) {
        AppUser appUser = getAppUser();
        BaseQueryVO vo = new BaseQueryVO();
        vo.setSchoolId(appUser.getTeacher().getSchoolId());
        List<StudentRoutineParam> list = studentRoutineParamService.selectList(vo);
        model.addAttribute("list", list);
        return "studentPoints/typeList";
    }

    /**
     * 类别详情
     *
     * @param model
     * @param typeId
     * @return
     */
    @RequestMapping("type/detail/{typeId}")
    public String typeDetail(Model model, @PathVariable Integer typeId) {
        model.addAttribute("typeId", typeId);
        return "studentPoints/typeDetail";
    }

    /**
     * 值班组列表
     *
     * @return
     */
    @RequestMapping("groupList")
    public String groupList(Model model) {
        AppUser appUser = getAppUser();
        BaseQueryVO vo = new BaseQueryVO();
        vo.setSchoolId(appUser.getTeacher().getSchoolId());
        vo.setType(GroupType.ON_DUTY.getCode());
        List<GroupInfo> list = groupInfoService.selectList(vo);
        model.addAttribute("list", list);
        return "studentPoints/groupList";
    }

    /**
     * 值班组详情
     *
     * @param model
     * @param groupId
     * @return
     */
    @RequestMapping("group/detail/{groupId}")
    public String groupDetail(Model model, @PathVariable Integer groupId) {
        model.addAttribute("groupId", groupId);
        return "studentPoints/groupDetail";
    }

    @RequestMapping("routine/detail/{id}")
    public String routineDetail(Model model, @PathVariable Integer id) {
        StudentRoutineReqVO vo = new StudentRoutineReqVO();
        vo.setId(id);
        vo.setSchoolId(getAppUser().getTeacher().getSchoolId());
        model.addAttribute("detail", studentRoutineService.selectViewById(vo));
        return "studentPoints/routineDetail";
    }

    @RequestMapping("student")
    public String personalPoints() {
        return "studentPoints/studentPoint";
    }

    @RequestMapping("class")
    public String schoolPoints() {
        return "studentPoints/classPoint";
    }

    /**
     * 类别详情
     *
     * @param typeId
     * @return
     */
    @RequestMapping("type/detail/query")
    @ResponseBody
    Object typeDetailQuery(Integer typeId) {
        return studentRoutineParamService.selectById(typeId);
    }

    /**
     * 值班组详情
     *
     * @param groupId
     * @return
     */
    @RequestMapping("group/detail/query")
    @ResponseBody
    Object groupDetailQuery(Integer groupId) {
        return groupInfoService.selectByPrimaryKey(groupId);
    }

    /**
     * 类别修改
     *
     * @param record
     * @return
     */
    @RequestMapping("type/addUpdate")
    @ResponseBody
    Object typeUpdate(StudentRoutineParam record) {
        try {
            AppUser appUser = getAppUser();
            record.setSchoolId(appUser.getTeacher().getSchoolId());
            studentRoutineParamService.addUpdate(record);
            return new RspResult(Status.SUCCESS);
        } catch (Exception e) {
            logger.error("---typeUpdate error={}", e);
        }
        return new RspResult(Status.FAIL);
    }

    /**
     * 类别修改
     *
     * @param record
     * @return
     */
    @RequestMapping("group/addUpdate")
    @ResponseBody
    Object groupUpdate(GroupInfo record) {
        try {
            AppUser appUser = getAppUser();
            record.setSchoolId(appUser.getTeacher().getSchoolId());
            record.setGroupType(GroupType.ON_DUTY.getCode());
            groupInfoService.addUpdate(record);
            return new RspResult(Status.SUCCESS);
        } catch (Exception e) {
            logger.error("---typeUpdate error={}", e);
        }
        return new RspResult(Status.FAIL);
    }

    @RequestMapping("register/power")
    @ResponseBody
    Object registerPower() {
        AppUser appUser = getAppUser();
        TeacherInfo teacherInfo = new TeacherInfo();
        teacherInfo.setId(appUser.getTeacher().getId());
        teacherInfo.setSchoolId(appUser.getTeacher().getSchoolId());
        return studentRoutineParamService.selectByTeacherId(teacherInfo);
    }

    /**
     * 学生常规登记提交
     *
     * @param vo
     * @return
     */
    @RequestMapping("register/submit")
    @ResponseBody
    Object registerSubmit(StudentRoutineReqVO vo) {
        try {
            AppUser appUser = getAppUser();
            vo.setJudgeTeacherId(appUser.getTeacher().getId());
            vo.setTermId(getCurrentTerm().getId());
            vo.setSchoolId(appUser.getTeacher().getSchoolId());
            studentRoutineService.addStudentRoutine(vo);
            return new RspResult(Status.SUCCESS);
        } catch (Exception e) {
            logger.error("--registerSubmit error={}", e);
        }
        return new RspResult(Status.FAIL);
    }

    @RequestMapping("group/list/query")
    @ResponseBody
    Object groupListQuery() {
        AppUser appUser = getAppUser();
        BaseQueryVO vo = new BaseQueryVO();
        vo.setSchoolId(appUser.getTeacher().getSchoolId());
        vo.setType(GroupType.ON_DUTY.getCode());
        return groupInfoService.selectList(vo);
    }

    @RequestMapping("totalPoint")
    @ResponseBody
    Object totalPoint(Integer keyId) {
        PointsReqVO vo = new PointsReqVO();
        vo.setTermId(getCurrentTerm().getId());
        vo.setKeyId(keyId);
        vo.setSchoolId(getAppUser().getTeacher().getSchoolId());
        return studentPointService.selectTotalPointForStudent(vo);
    }

    @RequestMapping("class/totalPoint")
    @ResponseBody
    Object classTotalPoint(Integer keyId) {
        PointsReqVO vo = new PointsReqVO();
        vo.setTermId(getCurrentTerm().getId());
        vo.setKeyId(keyId);
        vo.setSchoolId(getAppUser().getTeacher().getSchoolId());
        return classPointService.selectTotalPointForClass(vo);
    }

    @RequestMapping("routine/pageQuery")
    @ResponseBody
    Object routinePageQuery(PointsQueryVO vo) {
        try {
            Integer schoolId = getAppUser().getRoleType() == 1 ? getAppUser().getTeacher().getSchoolId() : getAppUser().getParent().getSchoolId();
            Page<StudentRoutineView> page = new Page<StudentRoutineView>();
            Grid grid = new Grid();
            List<OrderBy> sorts = new ArrayList<>();
            vo.setSchoolId(schoolId);
            vo.setTermId(getCurrentTerm().getId());
            sorts.add(new OrderBy("CREATE_TIME", "desc"));
            page.setSorts(sorts);
            page.setParams(vo);
            page.setPageNo(vo.getPage());
            page.setPageSize(vo.getRows());
            List<StudentRoutineView> list = studentRoutineService.selectListByPage(page);
            grid.setRows(list);
            grid.setTotal(page.getTotalRecord());
            return grid;
        } catch (Exception e) {
            logger.error("---routinePageQuery error={}", e);
        }
        return null;
    }
}
